<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<div>
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <a href="javascript:void(0)" onclick="_commentList.fn.toAddComment()" class="btn btn-outline btn-primary btn-lg" role="button">新增评论</a>
                    <a href="javascript:void(0)" onclick="_commentList.fn.batchDel()" class="btn btn-outline btn-danger btn-lg" role="button">一键删除</a>

                    <form class="navbar-form navbar-right" role="search">
                        <div class="form-group">
                            <input type="text" class="form-control" id="commentContent" maxlength="20" placeholder="评论内容"/>
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" id="commentUserName" maxlength="20" placeholder="用户名"/>
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control input-append date form_datetime" style="width: 180px;" readonly id="commentStartTime" maxlength="20" placeholder="开始时间"/>
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control input-append date form_datetime" style="width: 180px;" readonly id="commentEndTime" maxlength="20" placeholder="结束时间"/>
                        </div>
                        <button type="button" id="comment_search" class="btn btn-primary btn-sm">搜索</button>
                    </form>
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">
                    <div class="table-responsive">
                        <table class="table table-striped table-bordered table-hover" id="dataTables_comment">
                            <colgroup>
                                <col class="gradeA odd" style="width: 6%"/>
                                <col class="gradeA even"/>
                                <col class="gradeA odd"/>
                                <col class="gradeA even"/>
                                <col class="gradeA odd"/>
                            </colgroup>
                            <thead>
                            <tr>
                                <th><input type="checkbox" onclick="$leoman.checkAll(this)" class="checkall"/></th>
                                <th>用户名</th>
                                <th>评论时间</th>
                                <th>评论内容</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>

                </div>
                <!-- /.panel-body -->

            </div>
            <!-- /.panel -->
        </div>
    </div>


</div>
<!-- /#page-wrapper -->

<!-- Modal -->
<div class="modal fade" id="commentDelModal" tabindex="-1" role="dialog" aria-labelledby="pwdModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">删除提示</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" id="allCommentId"/>
                确定删除该评论？
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" onclick="_commentList.fn.subDelInfo()" class="btn btn-primary">确定</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    var _commentList = {
        v: {
            id: "_commentList",
            list: [],
            dTable: null
        },
        fn: {
            init: function () {
                $('.form_datetime').datetimepicker({
                    language: 'zh-CN',
                    weekStart: 1,
                    todayBtn: 1,
                    autoclose: 1,
                    todayHighlight: 1,
                    startView: 2,
                    forceParse: 0,
                    showMeridian: 1,
                    format: 'yyyy-mm-dd hh:ii:ss'
                });

                _commentList.fn.dataTableInit();

                // 查询
                $("#comment_search").click(function () {
                    _commentList.v.dTable.ajax.reload();
                });
            },
            dataTableInit: function () {
                _commentList.v.dTable = $leoman.dataTable($('#dataTables_comment'), {
                    "processing": true,
                    "serverSide": true,
                    "searching": false,
                    "ordering": false,
                    "ajax": {
                        "url": "common/commentList",
                        "type": "POST"
                    },
                    "columns": [
                        {"data": "id"},
                        {
                            "data": null,
                            render: function (data) {
                                if (null != data.user && null != data.user.nickName) {
                                    return data.user.nickName;
                                } else {
                                    return "管理员";
                                }
                            }
                        },
                        {
                            "data": "createTime",
                            render: function (data) {
                                return formatJsonDateTime(data, 'yyyy-MM-dd hh:mm:ss');
                            }
                        },
                        {
                            "data": null,
                            render: function (data) {
                                if (null != data.content && '' != data.content && data.content.length > 10) {
                                    return data.content.substring(0, 10) + '...';
                                } else {
                                    return data.content;
                                }
                            }
                        },
                        {"data": ""}
                    ],
                    "columnDefs": [
                        {
                            "data": null,
                            "defaultContent": "<button type='button' title='查看' class='btn btn-primary btn-circle look'>" +
                            "<i class='fa fa-eye'></i>" +
                            "</button>" +
                            "&nbsp;&nbsp;" +
                            "<button type='button' title='删除' class='btn btn-danger btn-circle delete'>" +
                            "<i class='fa fa-trash'></i>" +
                            "</button>",
                            "targets": -1
                        }
                    ],
                    "createdRow": function (row, data, index) {
                        _commentList.v.list.push(data);
                        $('td', row).eq(0).html("<input type='checkbox' value=" + data.id + ">");
                    },
                    rowCallback: function (row, data) {
                        $('td', row).last().find(".look").click(function () {
                            window.location.href = 'common/comment/info?commentId=' + data.id + "&sourceId=" + $('#commentSourceId').val() + "&type=" + $('#commentType').val();
                        });

                        $('td', row).last().find(".delete").click(function () {
                            _commentList.fn.delInfo(data.id);
                        });
                    },
                    "fnServerParams": function (aoData) {
                        aoData.content = $('#commentContent').val();
                        aoData.userName = $('#commentUserName').val();
                        aoData.startTime = $('#commentStartTime').val();
                        aoData.endTime = $('#commentEndTime').val();
                        aoData.sourceId = $('#commentSourceId').val();
                        aoData.type = $('#commentType').val();
                    },
                    "fnDrawCallback": function (row) {
                        $leoman.uiform();
                    }
                });
            },
            addInfo: function () {
                $leoman.clearForm($('#infoForm'));

                $("#infoModal").modal("show");
            },
            delInfo: function (id) {
                $('#allCommentId').val(id);
                $("#commentDelModal").modal("show");
            },
            subDelInfo: function () {
                var allCommentId = $('#allCommentId').val();

                $leoman.ajax("common/comment/delete", {
                    "commentId": allCommentId
                }, function (result) {
                    if (result == 1) {
                        $leoman.notify("操作成功", "success");
                        $("#commentDelModal").modal("hide");
                        _commentList.v.dTable.ajax.reload(null, false);
                    } else {
                        $leoman.notify("操作失败", "error");
                    }
                });
            },
            subInfo: function () {
                var flag = true;
                var content = $('#content').val();

                if (null == content || content == '') {
                    $leoman.notify("请输入评论名称", "error");
                    flag = false;
                    return;
                }

                // 所有的验证通过后，执行新增操作
                if (flag) {
                    $leoman.ajax("backend/tag/save", {
                        "allCommentId": $('#allCommentId').val(),
                        "content": content
                    }, function (result) {
                        if (result > 0) {
                            $leoman.notify("操作成功", "success");
                            $("#infoModal").modal("hide");
                            $('#content').val('');
                            _commentList.v.dTable.ajax.reload(null, false);
                        } else {
                            $leoman.notify("操作失败", "error");
                        }
                    });
                }
            },
            batchDel: function () {
                var checkBox = $("#dataTables_comment tbody tr").find('input[type=checkbox]:checked');
                var ids = checkBox.getInputId();
                _commentList.fn.deleteRow(checkBox, ids)
            },
            deleteRow: function (checkBox, ids) {
                if (ids.length > 0) {
                    $leoman.optNotify(function () {
                        $leoman.ajax("common/comment/batchDelete", {
                            ids: JSON.stringify(ids)
                        }, function (result) {
                            if (result > 0) {
                                $leoman.notify("操作成功", "success");
                                _commentList.v.dTable.ajax.reload();
                            } else {
                                $leoman.notify("操作失败", "error");
                            }
                        })
                    }, '确定删除选中的所有评论？', '确定');
                }
            },
            toAddComment: function () {
                window.location.href = 'common/comment/info?sourceId=' + $('#commentSourceId').val() + '&type=' + $('#commentType').val();
            }
        }
    }

    $(document).ready(function () {
        _commentList.fn.init();
    });

</script>

</html>